<?php 
$now = time();
?>
<form name="cmj" action="<?=$HOME_?>" method="post">
<fieldset style="width: 400px;"><legend>CMJ report options</legend>
<table>
<tr><td style="align: center; font-weight: bold; text-align: center; width: 160px; ">category</td><td style="align: center; font-weight: bold; text-align: center; ">minimum add date</td></tr>
<tr>
   <td rowspan="2"><?php 

if ($_REQUEST[cmjMaxResult]) $cmjMaxResult = $_REQUEST[cmjMaxResult];
else $cmjMaxResult = 100;
?>
<?php 
if ($_REQUEST[statCat]) $statCat = $_REQUEST[statCat];
?>
<select name="statCat">
<option value="all" <?php 
if ($statCat === 'all') { 
   echo (' selected="selected" ');
   $catSet = true;
}
?>>all</option>
<?php
$catQuery = mysql_query("SELECT `id`,`category`,`default` FROM `category` ORDER BY `category`");
while ($cat = mysql_fetch_array($catQuery)) {
?><option value="<?=$cat[id]?>"<?php 
if ($cat[id] == $statCat) {
   echo( 'selected="selected"');
   $catSet = true;
} else if ($cat['default'] && !$catSet) print(' selected="selected"');
?>><?=$cat[category]?></option>
<?php
}
?></select>
<br />
<select name="cmjMaxResult">
<option value="25"  <?php if ($cmjMaxResult == 25) echo(' selected="selected"')?>>25</option>
<option value="50"  <?php if ($cmjMaxResult == 50) echo(' selected="selected"')?>>50</option>
<option value="100"  <?php if ($cmjMaxResult == 100) echo(' selected="selected"')?>>100</option>
<option value="200"  <?php if ($cmjMaxResult == 200) echo(' selected="selected"')?>>200</option>
</select> results
<br /></td>
   <td><input type="radio" name="weeksOldType" onClick="if (this.checked) document.cmj.weeksOld.disabled = false; " value="number" <?php 
   if ($_REQUEST[weeksOldType] === 'number' || !$_REQUEST[weeksOldType]) echo (' checked="checked" ');
   ?> /> 
   <input type="text" name="weeksOld" value="<?php 
   if ($_REQUEST[weeksOld]) echo ($_REQUEST[weeksOld]);
   else echo('12');
   ?>" <?php 
   if ($_REQUEST[weeksOldType] === 'all') echo(' disabled="disabled" '); 
   ?> style="width: 30px;" /> weeks old (by add date)</td>
</tr>
<tr>
   <td><input onClick="if (this.checked) document.cmj.weeksOld.disabled = true; " type="radio" name="weeksOldType" value="all" <?php
   if ($_REQUEST[weeksOldType] === 'all') echo (' checked="checked" '); 
   ?> /> all add dates</td>
</tr>
</table>

<input type="hidden" name="loading" value="true" />
<input type="submit" value="get stats" /></fieldset>
</form>
<?php 

//$ = date('Y-m-d H:i:s', mktime(date(H), date(i), date(s), date(m)-3, date(d), date(Y)));

if (!$_REQUEST[airStartTime]) $airStartTime = $statStartTime;
else $airStartTime = $_REQUEST[airStartTime];

if ( ($_REQUEST[weeksOldType] === 'number') && ($_REQUEST[weeksOld]) ) $addDateMin = date('Y-m-d', mktime(date(H, $now), date(i, $now), date(s, $now), date(m, $now), ( (date(d, $now)) - ( $_REQUEST[weeksOld] * 7) ), date(Y, $now)));
else if ( $_REQUEST[weeksOldType] === 'all' ) $addDateMin = '0000-00-00';
else $addDateMin = date('Y-m-d', mktime(date(H, $now), date(i, $now), date(s, $now), date(m, $now)-3, date(d, $now), date(Y, $now)));

// the airAlbRank() function returns an array of ranked albIds and spin counts
// described in context in ../includes/libccramp.inc, section statistics 
// inputs: min-air-datetime, max-air-datetime, result-max, cat-id, status, min-add-date, request
$cmj = airAlbRank($airStartTime, false, $cmjMaxResult, $statCat, false, $addDateMin);

?>


<form name="cmjList" action="<?php echo($_HOME)?>?mode=cmj" metho="post">
<table>
<thead><tr><th></th><th>Rank</th><th>Spins</th><th>Artist // Album</th></tr></thead>
<tbody id="cmjListTbody">
<?php 
$i=0;
while ( (count($cmj) > $i) && ($i < $cmjMaxResult) ) {
   $albSql = " SELECT * FROM `album` WHERE (`cddbid`='".$cmj[$i][albId]."') LIMIT 1 ";
   $albQuery = mysql_query($albSql);
   $alb = mysql_fetch_array($albQuery);
   $row = $i+1;
   ?>
   <tr>
   <td><?php 
   if ($i != 0) {
      ?><a onClick="cmjMvAlbUp(<?php echo($row) ?>);"><img src="./themes/<?=$theme?>/img/up.png" alt="arrow, up" title="move up" /></a><?php 
   }
   ?>
      <input type="hidden" name="cmj[<?php echo($row) ?>][albId]" id="cmj[<?php echo($row) ?>][albId]" value="<?php echo($cmj[$i][albId]) ?>" />
      <input type="hidden" name="cmj[<?php echo($row) ?>][spins]" id="cmj[<?php echo($row) ?>][spins]" value="<?php echo($cmj[$i][spins]) ?>" />
   </td>
   <td><?php echo($row) ?></td>
   <td><?php echo($cmj[$i][spins]) ?></td>
   <td><?php echo($alb[artist]) ?> (<?php echo($alb[title]) ?>)</td>
   </tr>
   <?php 
//   printf('<tr><td>%s</td><td>%s</td><td>%s (%s)</td></tr>', $i+1, $cmj[$i][spins], $alb[artist], $alb[title]);
   $i++;
}
?>
   <tr><td></td><td colspan="3"><a onClick="cmjTsvExport();" href="./OUTGOING/cmj.csv">export as tsv</a></td></tr>
</tbody>
</table>
<input type="hidden" name="rowLength" id="cmjListRowLength" value="<?php echo($i) ?>" />
</form>

<?php 

//$cmjCsvContents = "\n";

$i=0;
while ( (count($cmj) > $i) && ($i < $cmjMaxResult) ) {
   $albSql = " SELECT * FROM `album` WHERE (`cddbid`='".$cmj[$i][albId]."') LIMIT 1 ";
   $albQuery = mysql_query($albSql);
   $alb = mysql_fetch_array($albQuery);
   $cmjCsvContents .= ($i+1) .'	'."$alb[artist]".'	'."$alb[title]".'	'."$alb[label]"."\n";
   $i++;
}

fwrite(fopen('./OUTGOING/cmj.csv', 'w'), $cmjCsvContents );
?>
<a href="OUTGOING/cmj.csv">csv file</a>

<br />
<br />
<h2>Requests:</h2><br />
<?php 

// the airAlbRank() function returns an array of ranked albIds and spin counts
// described in context in ../includes/libccramp.inc, section statistics 
// inputs: min-air-datetime, max-air-datetime, result-max, cat-id, status, min-add-date, request
$cmjReq = airAlbRank($airStartTime, false, $cmjMaxResult, $statCat, false, $addDateMin, true);


//$statReqSql = $statSql." AND (`request`='1') ".$statSqlOrderBy;
//$statReqQuery = mysql_query($statReqSql);

//$i = -1;
//while ($statReq = mysql_fetch_array($statReqQuery)) {
//   if ($statReq[cddbid] == $cmjReq[$i][albId]) {
//      $cmjReq[$i][addDate] = $statReq[add_date]; // FYI: this will overwrite add-date discrepancies with the latest/updated ones for all instances 
//      $cmjReq[$i][spins]++;
//   } else {
//      $i++;
//      $cmjReq[$i][albId] = $statReq[cddbid];
//      $cmjReq[$i][addDate] = $statReq[add_date];
//      $cmjReq[$i][spins] = 1;
//   }
//}
if ($cmjReq) {
   // swap database data into columns, because arraymultisort needs them
   foreach ($cmjReq as $key => $row) {
      $albId[$key]  = $row['albId'];
      $spins[$key] = $row['spins'];
      $addDate[$key] = $row['addDate'];
   }
   // array_multisort() returns the columns as an array, with the most spin counts first
   @array_multisort($spins, SORT_DESC, $addDate, SORT_DESC, $albId, SORT_DESC, $cmjReq);
   
} else {
   echo ('Nothing meeting those conditions got any requests. <br />');
}
?>
<table>
<thead><tr><th>Rank</th><th>Requests</th><th>Artist // Album</th></tr></thead>
<tbody>
<?php 
$i=0;
while ( (count($cmjReq) > $i) && ($i < $cmjMaxResult) ) {
   $albSql = " SELECT * FROM `album` WHERE (`cddbid`='".$cmjReq[$i][albId]."') LIMIT 1 ";
   $albQuery = mysql_query($albSql);
   $alb = mysql_fetch_array($albQuery);
   printf('<tr><td>%s</td><td>%s</td><td>%s (%s)</td></tr>', $i+1, $cmjReq[$i][spins], $alb[artist], $alb[title]);
//   if ($i > 1) break 1;
   $i++;
}
?>
</tbody>
</table>